package com.memogame;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class StartKinectMemoGame extends Activity {

	private static String METHOD_NAME = "RunKinectMemoGame";
	private static String SOAP_ACTION = Data.NAMESPACE + METHOD_NAME;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start_kinect_memo_game);
		
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_start_kinect_memo_game, menu);
		return true;
	}


	public void buttonClicked(View v) {

		if(v.getId() == R.id.button1) {
			String command;	
			//RadioButton radioNormal = (RadioButton) findViewById(R.id.radioNormal);
			RadioButton radioMotion = (RadioButton) findViewById(R.id.radioMotion);
			RadioButton radioVoice = (RadioButton) findViewById(R.id.radioVoice);
			if(radioMotion.isChecked()) {
				command = "/motion";
				Log.d(Data.TAG, "z1 motion");
			} else if(radioVoice.isChecked()) {
				command = "/voice";
				Log.d(Data.TAG, "z1 voice");
			} else {
				command = "";
				Log.d(Data.TAG, "z1 normal");
			}
					
			TextView text = new TextView(this);
			AsyncCallWS task = new AsyncCallWS(text, command);
			task.execute();
		}
	}

	private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

		TextView text;
		boolean result;
		String command;

		public AsyncCallWS(TextView text, String command) {
			this.text = text;
			this.command = command;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			this.result = getResult(this.command);
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			text.setTextSize(18);
			if (this.result) {
				text.setText("Game has been run...");
			} else {
				text.setText("Cannot run a game...");
			}
			text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

			setContentView(text);
		}

	}

	private boolean getResult(String command) {
		SoapObject Request = new SoapObject(Data.NAMESPACE, METHOD_NAME);

		boolean result = false;
		PropertyInfo pi = new PropertyInfo();
		pi.setName("command");
		pi.setValue(command);
		pi.setType(int.class);
		Request.addProperty(pi);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(Request);

		try {
			AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
					Data.URL);

			androidHttpTransport.call(SOAP_ACTION, envelope);
			result = Boolean.valueOf(envelope.getResponse().toString());
			Log.d(Data.TAG, "z1 result: " + result);

		} catch (Exception e) {
			Log.e(Data.TAG, "z1 error: " + e.getMessage());
			e.printStackTrace();
		}
		return result;
	}

}
